//
//  GMChangePasswordViewController.m
//  GuardMan
//
//  Created by steven on 2016/12/25.
//  Copyright © 2016年 steven. All rights reserved.
//

#import "GMChangePasswordViewController.h"
#import "GMAuthView.h"
#import "GMButton.h"
#import "GMAuthTextField.h"
#import "GMChangePasswordDataController.h"

@interface GMChangePasswordViewController ()

@property (nonatomic, strong)GMAuthView *authView;

@property (nonatomic, strong)GMChangePasswordDataController *changePasswordDataController;

@end

@implementation GMChangePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.changePasswordDataController = [[GMChangePasswordDataController alloc] init];
    
    [self setupContentView];
}

#pragma mark - setup content view
- (void)setupContentView {
    self.view.backgroundColor = [UIColor whiteColor];
    // set navigation title
    [self.navigationItem setTitle:@"修改密码"];
    
    // auth base view
    [self.view addSubview:self.authView];
    [self.authView autoPinEdgesToSuperviewEdges];
    
    // first password text field
    [self.authView addSubview:self.firstPasswdTextField];
    [self.firstPasswdTextField autoAlignAxisToSuperviewAxis:ALAxisVertical];
    [self.firstPasswdTextField autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
    [self.firstPasswdTextField autoSetDimensionsToSize:CGSizeMake(self.view.frame.size.width*0.8,AUTH_TEXT_FIELD_HEIGHT)];
    
    // second password text field
    [self.authView addSubview:self.secondPasswordTextField];
    [self.secondPasswordTextField autoAlignAxisToSuperviewAxis:ALAxisVertical];
    [self.secondPasswordTextField autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.firstPasswdTextField withOffset:14];
    [self.secondPasswordTextField autoSetDimensionsToSize:CGSizeMake(self.view.frame.size.width*0.8, AUTH_TEXT_FIELD_HEIGHT)];

    // second password text field
    [self.authView addSubview:self.originPasswdTextField];
    [self.originPasswdTextField autoAlignAxisToSuperviewAxis:ALAxisVertical];
    [self.originPasswdTextField autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:self.firstPasswdTextField withOffset:-14];
    [self.originPasswdTextField autoSetDimensionsToSize:CGSizeMake(self.view.frame.size.width*0.8, AUTH_TEXT_FIELD_HEIGHT)];
    
    // register button
    [self.authView addSubview:self.confirmChangeButton];
    [self.confirmChangeButton autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.secondPasswordTextField withOffset:14];
    [self.confirmChangeButton autoAlignAxisToSuperviewAxis:ALAxisVertical];
    [self.confirmChangeButton autoSetDimensionsToSize:CGSizeMake(self.view.frame.size.width*0.8, AUTH_TEXT_FIELD_HEIGHT)];
}
#pragma mark - register button pressed
- (void)confirmChangeButtonPressed {
    // hide keyboard
    [self.originPasswdTextField resignFirstResponder];
    [self.firstPasswdTextField resignFirstResponder];
    [self.secondPasswordTextField resignFirstResponder];

    NSString *originPassword = self.originPasswdTextField.text;
    NSString *password = self.firstPasswdTextField.text;
    NSString *secondPassword = self.secondPasswordTextField.text;
    if (originPassword.length == 0) {
        [self showAlertMessage:@"请输入原密码"];
    } else if (password.length == 0) {
        [self showAlertMessage:@"请输入密码"];
    } else if(secondPassword.length == 0) {
        [self showAlertMessage:@"请输入确认密码"];
    } else if (![password isEqualToString:secondPassword]) {
        [self showAlertMessage:@"密码不一致"];
    } else {
        // change password here
        NSDictionary *params = @{@"password":originPassword, @"resetPassword":password};
        [self.changePasswordDataController changePasswordWithParams:params callback:^(NSString *error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (error == nil) {
                    [self renderSuccessView];
                } else {
                    [self showAlertMessage:error];
                }
            });
        }];
    }
}

#pragma mark - render success view
- (void)renderSuccessView {
    NSDictionary *dict = self.changePasswordDataController.response[@"data"];
    NSString *token = dict[@"token"];
    if (token) {
        [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"token"];
        [self showAlertMessage:@"密码修改成功"];
    } else {
        [self showAlertMessage:@"密码修改失败"];
    }
    
}

// Getter for authView
- (GMAuthView *)authView {
    if (!_authView) {
        _authView = [[GMAuthView alloc] init];
    }
    
    return _authView;
}

// Getter for first passwrod text field
- (GMAuthTextField *)originPasswdTextField {
    if (!_originPasswdTextField) {
        _originPasswdTextField = [[GMAuthTextField alloc] init];
        _originPasswdTextField.secureTextEntry = YES;
        _originPasswdTextField.placeholder = @"原密码";
    }

    return _originPasswdTextField;
}

// Getter for first passwrod text field
- (GMAuthTextField *)firstPasswdTextField {
    if (!_firstPasswdTextField) {
        _firstPasswdTextField = [[GMAuthTextField alloc] init];
        _firstPasswdTextField.secureTextEntry = YES;
        _firstPasswdTextField.placeholder = @"新密码";
    }
    
    return _firstPasswdTextField;
}

// Getter for second passwrod text field
- (GMAuthTextField *)secondPasswordTextField {
    if (!_secondPasswordTextField) {
        _secondPasswordTextField = [[GMAuthTextField alloc] init];
        _secondPasswordTextField.secureTextEntry = YES;
        _secondPasswordTextField.placeholder = @"确认新密码";
    }
    
    return _secondPasswordTextField;
}

// Getter for register button
- (GMButton *)confirmChangeButton {
    if (!_confirmChangeButton) {
        _confirmChangeButton = [[GMButton alloc] init];
        [_confirmChangeButton setTitle:@"确认提交" forState:UIControlStateNormal];
        [_confirmChangeButton addTarget:self action:@selector(confirmChangeButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _confirmChangeButton;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
